// JavaScript Document
$(document).ready(function(e) {
    
	query_case_categorys();
	
	function subMenu_mouseover_handler(e) {
		$(this).addClass("over");
	}
	
	function subMenu_mouseout_handler(e) {
		$(this).removeClass("over");
	}
	
	var clickSubMenu;
	var categoryId;
	function subMenu_click_handler(e) {
		if(clickSubMenu) {
			clickSubMenu.removeClass("down");
		}
		clickSubMenu = $(this);
		clickSubMenu.addClass("down");	
		$("div#caseDetail").hide();
		$("div#caseList").show();
		$("div#page").show();
		categoryId = $(this).attr("data-id");
		var params = [{property:"l.languageId", value:languageId, condition:"eq"},
		              {property:"p.successfulCaseCategoryId", value:categoryId, condition:"eq"},
		              {property:"p.successfulCaseStatus", value:1, condition:"eq"}];
		queryCases(params, 4, 1);
	}	
	
	function query_case_categorys() {
		$.getJSON(ctx + "/successfulCase/getCaseCategorys.do?languageId=" + languageId, function(data){ 
			if(data) {
				$("ul#category").empty();
				$.each(data, function(i, category) {
					category_handler(i, category);
				});
			}
		});
	}
	
	function category_handler(i, d) {
		var li = $("<li>").addClass("menu")
						.attr("data-id", d.productCategoryId)
						.text(d.categoryName)
						.on("click", subMenu_click_handler)
						.on("mousemove",subMenu_mouseover_handler)
					    .on("mouseout",subMenu_mouseout_handler);
		$("ul#category").append(li);
		if(i == 0) {
			li.click();
		}
	}
	
	var cases;
	function queryCases(params, pageSize, pageNo) {
		$.ajax ({
			url : ctx + "/successfulCase/query.do",
			type : "post",
			dataType:"json",	
			data : {
				pageNo : pageNo,
				pageSize : pageSize,
				orderType : "desc",
				orderBy : "isTop",
				rules : $.toJSON(params)
			},
			success : function(response) {
				cases = response.result;
				updatePageLabel(response.pageNo,Math.ceil(response.totalCount/pageSize));
				query_cases_handler(response.result);
			},
			error : function(response) {
				alert(response.status + " - " + response.statusText);
			}
		});		
	}
	
	function query_cases_handler(result) {
		$("div#caseList").empty();
		$.each(result, function(index, _case) {
			var found = false;
			var div = $("<div>").addClass("product")
						  .append($("<p>").addClass("desc").text(_case.successfulCaseModel))
						  .append($("<p>").addClass("title").text(_case.successfulCaseName))
						  .attr("data-pid", _case.successfulCaseId)
						  .on("click", query_case_byid_handler);
			if(_case.files.length > 0) {
				$.each(_case.files, function(i, file) {
					if(1 == isMainFile) {
						found = true;
						div.append($("<img>").css("padding","12px 1px 1px 2px")
							 .attr("width","355px").attr("height","180px")
							 .attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + file.uploadFileInfoId)
							 .attr("alt", file.fileName));						
					}
				});
				if(!found) {
					div.append($("<img>").css("padding","12px 1px 1px 2px")
						 .attr("width","355px").attr("height","180px")
						 .attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + _case.files[0].uploadFileInfoId)
						 .attr("alt", _case.files[0].fileName));
				}
			}						  
			$("div#caseList").append(div);
		});
	}	
	
	function query_case_byid_handler(e) {
		$("div#caseList").hide(); 
		$("div#page").hide();
		$("div#caseDetail").show();
		var pid = $(this).attr("data-pid");
		$.ajax({
			url : ctx + "/successfulCase/web/successfulCaseDetail/" + pid + ".do",
			type : "post",
			dataType:"html",
			success : function(response) {
				$("div#detail p").html(response);
			},
			error : function(response) {
				alert(resrponse.status + " - " + response.statusText);
			}		
		});
		$.each(cases, function(index, _case) {	
			if(pid == _case.successfulCaseId) {
				$("div.caseName").text(_case.successfulCaseName);
				$("div#date").text(_case.successfulCaseModel);
				return false;
			}
		});
	}
	
	//分頁處理 --- start
	var begin = 0;
	function updatePageLabel(pageNo, totalPage) {
		$("#tr_page").empty();
		if(totalPage == 0) {		
			return;
		}
		if(totalPage <= 5) {
			createPageLabel(1,(totalPage+1),pageNo);
		} else {
			if((pageNo < 5)) {
				createPageLabel(1,6,pageNo);
			} else { 
				if(((pageNo - 3) % 2) == 0) {
					begin = (pageNo + 2) > totalPage ? (totalPage - 4) : pageNo - 2;
				} 
				createPageLabel(begin,(begin+5),pageNo);
			}				
			if((pageNo + 2) < totalPage) {
				$("<td style='color:#666666'>...</td>").appendTo("#tr_page");
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:totalPage}).text(totalPage))
						 .appendTo("#tr_page");
			}
		}
		$("<td style='color:#666666'>&gt;</td>").appendTo("#tr_page");
		pageClick();
	}
	
	function createPageLabel(begin, end, pageNo) {
		$("<td style='color:#666666'>&lt;</td>").appendTo("#tr_page");
		for(var i=begin; i<end; i++) {
			if(i != pageNo) {
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:i}).text(i)).appendTo("#tr_page");
			} else {
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:i}).text(i).css("color","#F00"))
						 .appendTo("#tr_page");
			}
		}
	}
	
	function pageClick() {
		$("#tr_page a").on("click",function(e) {
			queryCases([{property:"l.languageId", value:languageId, condition:"eq"},
		                {property:"p.successfulCaseCategoryId", value:categoryId, condition:"eq"},
			            {property:"p.successfulCaseStatus", value:1, condition:"eq"}], 
			            4, 
			            parseInt($(this).attr("title")));
		});
	}	
	//分頁處理end
	
});